<?php
namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;

class Order extends Backend {
    
    public function _initialize(){
        parent::_initialize();
        
        $this->model = model('Order');
        
        $this->assign('pay_type', array_merge([0 => '请选择'], config('pay_type')));
        $this->assign('pay_event', array_merge([0 => '请选择'], config('pay_event')));
    }
    
    public function index(){
        //设置过滤方法
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $total = $this->model->where($where)
                ->where(['is_del'=>0])
                ->order($sort, $order)
                ->count();
            
            $list = $this->model->where($where)
                ->where(['is_del'=>0])
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            
            return json($result);
        }
        return $this->view->fetch();
    }
    
    public function del($ids = ''){
        if($ids){
            $pk = $this->model->getPk();
            
            Db::startTrans();
            try{
                $this->model->where($pk, 'IN', $ids)->update(['is_del'=>1, 'update_time'=>time()]);
                Db::commit();
                $this->success();
            } catch (\think\exception\PDOException $e) {
                // 回滚事务
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    
}